﻿namespace Hidistro.UI.SaleSystem.CodeBehind
{
    using Hidistro.Core;
    using Hidistro.Core.Enums;
    using Hidistro.Entities.Commodities;
    using Hidistro.Membership.Context;
    using Hidistro.SaleSystem.Catalog;
    using Hidistro.SqlDal.Members;
    using Hidistro.UI.Common.Controls;
    using Hidistro.UI.SaleSystem.Tags;
    using JSShop.Model;
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.IO;
    using System.Text;
    using System.Web;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using wnxd.javascript;

    public class SubPage_new : HtmlTemplatedWebControl
    {
        private Juse.Interface.ProductBrowser ProductBrowser = new Juse.Interface.ProductBrowser();
        private HtmlGenericControl clist;
        private wnxd_SortList list;
        private wnxd_Product_List product_new;
        //private wnxd_Product_List product_cnxh;
        private wnxd_Product_List product_group;
        private wnxd_Product_List product_jrtj;
        private wnxd_Product_List product_cnxh;
        private HtmlInputHidden hide_cid;
        private Literal seo;
        private wnxd_Shopping_LiList brandlist;
        private HtmlGenericControl zuocedaohang;
        private int categoryId;
        public SubPage_new()
        {
            int.TryParse(HttpContext.Current.Request.QueryString["CategoryId"], out this.categoryId);
            CategoryInfo category = CategoryBrowser.GetCategory(this.categoryId);
            if (((category != null) && (category.Depth == 1)) && !(string.IsNullOrEmpty(category.Theme) || !File.Exists(HttpContext.Current.Request.MapPath(HiContext.Current.GetSkinPath() + "/categorythemes/" + category.Theme))))
            {
                this.SkinName = "/categorythemes/" + category.Theme;
            }
            this.LoadPageSearch(category);
        }
        private void LoadPageSearch(CategoryInfo category)
        {
            if (!string.IsNullOrEmpty(category.MetaKeywords))
            {
                MetaTags.AddMetaKeywords(category.MetaKeywords, HiContext.Current.Context);
            }
            if (!string.IsNullOrEmpty(category.MetaDescription))
            {
                MetaTags.AddMetaDescription(category.MetaDescription, HiContext.Current.Context);
            }
            if (!string.IsNullOrEmpty(category.MetaTitle))
            {
                PageTitle.AddSiteNameTitle(category.MetaTitle);
            }
            else
            {
                PageTitle.AddSiteNameTitle(category.Name);
            }
        }
        protected override void OnInit(EventArgs e)
        {
            if (this.SkinName == null)
            {
                this.SkinName = "Skin-SubPage_new.html";
            }
            base.OnInit(e);
        }

        protected override void AttachChildControls()
        {
            this.clist = (HtmlGenericControl)this.FindControl("clist");
            this.list = (wnxd_SortList)this.FindControl("list");
            this.product_new = (wnxd_Product_List)this.FindControl("product_new");
            //this.product_cnxh = (wnxd_Product_List)this.FindControl("product_cnxh");
            this.product_group = (wnxd_Product_List)this.FindControl("product_group");
            this.product_jrtj = (wnxd_Product_List)this.FindControl("product_jrtj");
            this.product_cnxh = (wnxd_Product_List)this.FindControl("product_cnxh");
            this.hide_cid = (HtmlInputHidden)this.FindControl("hide_cid");
            this.seo = (Literal)this.FindControl("seo");
            this.brandlist = (wnxd_Shopping_LiList)this.FindControl("brandlist");
            this.zuocedaohang = (HtmlGenericControl)this.FindControl("zuocedaohang");
            if (!this.Page.IsPostBack)
            {
                this.hide_cid.Value = this.categoryId.ToString();
                List<CategoryInfo> list = CategoryBrowser.GetMaxSubCategories(this.categoryId, 0x3e8) as List<CategoryInfo>;

                for (int i = 0; i < list.Count; i++)
                {
                    list[i].louc = i;
                    this.clist.InnerHtml += string.Format("<a href=\"{0}\">{1}</a>", Globals.GetSiteUrls().StaticizeSubCategory((int)list[i].CategoryId, list[i].RewriteName,(int)list[i].ParentCategoryId, list[i].ParentRewriteName,0,0,0,0,0), list[i].Name);
                    this.zuocedaohang.InnerHtml += string.Format("<li><a class=\"num\" href=\"#fenleiid{0}\" style=\"display: block;\">{1}F</a><a class=\"word\" href=\"#fenleiid{0}\" style=\"display: none;\">{2}</a></li>", list[i].CategoryId, (i + 1).ToString(), this.InsertBr(list[i].Name, 5));
                }
                this.list.DataSource = list;
                this.list.DataBind();
                this.product_new.DataSource = ProductBrowser.GetOrderByProductByOneCategoryId(this.categoryId, "AddedDate desc", 5);
                this.product_new.DataBind();
                this.product_group.DataSource = ProductBrowser.GetOrderByProductByOneCategoryId(this.categoryId, "ShowSaleCounts desc", 5);
                this.product_group.DataBind();
                this.product_jrtj.DataSource = ProductBrowser.GetOrderByProductByOneCategoryId(this.categoryId, "AddedDate desc", 5);
                this.product_jrtj.DataBind();
                IList<int> browedProductList = BrowsedProductQueue.GetBrowedProductList();
                aspnet_Members member = null;
                Member m = HiContext.Current.User as Member;
                if (m != null)
                    member = new json(m).TryConvert(typeof(aspnet_Members)) as aspnet_Members;
                this.product_cnxh.DataSource = ProductBrowser.GetCaiNiXiHuan(member, browedProductList, 5);

                this.brandlist.DataSource = new Juse.Interface.CategoryBrowser().GetBrandList(this.categoryId, 13);
                this.brandlist.DataBind();
            }
        }

        private string InsertBr(string str, int len)
        {
            int n = 0;
            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] < 256) n++;
                else n += 2;
                if (n > len)
                {
                    str = str.Insert(i, "<br />");
                    break;
                }
            }
            return str;
        }
    }
}

